/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sUtils;

import javax.microedition.lcdui.*;
import java.util.*;

/**
 *
 * @author thomas.s
 */
public class MenuView extends Canvas {

    Vector guiobjs = new Vector(20,2);
    int menuW, menuH, menuX=0, menuY=0, itemH=60, fontH=20, itemY=0, border=5;
    float itemX=0;
    Image bg=null;
    Image nL=null,nM=null,nR=null, pL=null,pM=null,pR=null, nS=null, pS=null;


    class GUIObject extends Object {
        public int x, y, w, h;
        public String name, text;
        public boolean on, enabled, hitted, updated;
        public GUIObject() { on=false; enabled=false; hitted=false; name=null; }
        public void paint( Graphics g, int sx, int sy, int sw ) {}
        public boolean hitTest( int x, int y ) { return false; }
        public boolean drag( int mx, int my ) { return false; }
        public void setText( String t ) { text = t; updated=true; }
        public void setColor( int col ) {} 
        public void setOn( boolean o ) { on=o; updated=true; }
        public void setVal( double o ) {}
        public double getVal() { return 0; }
        public boolean equal( String n ) { return name==null ? false : name.equalsIgnoreCase(n); }
    }

    class Header extends GUIObject {

        Header( String _text, String _name, int y0 )
        {
            text=_text; h=20; y=y0; name = _name;
            w=menuW; x=0;
        }
        public void paint( Graphics g, int sx, int sy, int sw ) {
            g.setColor( 0x7f7f7f );
            g.drawRect(x+sx,y+sy,w,1);
            if (text!=null)
            {
                Font font = Font.getFont( Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_SMALL );
                g.setFont(font);
                g.setColor( 0xffffff );
                g.drawString(text, x+sx, y+sy+border, 0);
            }
            updated=false;
        }
    }

    class Button extends GUIObject {

        int color;

        public Button( String _text, String _name, int y0, double x0, double w0 )
        {
            w=(int)(w0*(double)menuW); x=(int)(x0*(double)menuW);
            h = itemH;
            y = y0;
            name = _name;
            text = _text;
            color = -1;
            enabled = true;
            hitted = false;
            on = false;
        }

        public void paint( Graphics g, int sx, int sy, int sw )
        {
            Font font;
            boolean down = on|hitted;
            int left = nL.getWidth();
            int right = nR.getWidth();
            int mid = nM.getWidth();
            g.drawImage(down ? pL : nL, x+sx+border, y+sy+border, 0);
            for ( int x0=left; x0<w-right-border*2; x0+=mid ) g.drawImage(down ? pM : nM, x0+x+sx+border, y+sy+border, 0);
            g.drawImage(down ? pR : nR, x+w-right+sx-border, y+sy+border, 0);
            if ( text.length()>0 )
            {
                if (color>=0) g.setColor(color); else g.setColor(down ? 0xffffff : 0x000000 );
                font = Font.getFont( Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, w<=200 ? Font.SIZE_MEDIUM : Font.SIZE_LARGE );
                g.setFont(font);
                int tw = font.stringWidth(text);
                int th = font.getHeight();
                g.drawString(text, x+w/2-tw/2+sx, y+h/2-th/2+sy, 0);
            }
            updated=false;
        }

        public boolean hitTest( int mx, int my )
        {
            hitted = ( mx>=x && mx<(x+w) && my>=y && my<(y+h) ); updated=true;
            return hitted;
        };

        public void setColor( int col ) { color=col; updated=true; }
     }


    class Slider extends GUIObject {

        double min, max, val;

        public Slider( String _text, String _name, int y0, double _min, double _max, double _val )
        {
            h = itemH;
            x = 0; y = y0;
            w = menuW;
            min = _min; max = _max; val = _val;
            text = _text;
            name = _name;
            enabled = true;
            hitted = false;
            on = false;
        }

        public void paint( Graphics g, int sx, int sy, int sw )
        {
            g.setColor(0xffffff);
            g.fillRect(sx+x+h/2,sy+h/2+y,sw-100-h,2);
            g.drawImage( hitted ? pS : nS, (int)(sx+((sw-100-h)*(val-min))/(max-min)+x), sy+y, 0);
            if ( text.length()>0 )
            {
                g.setColor(0xffffff);
                Font font = Font.getFont( Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_MEDIUM );
                g.setFont(font);
                int th = font.getHeight();
                g.drawString(Strings.toStr(val,2,1,' ',"",text), sx+w-100+x, sy+h/2-th/2+y, 0);
            }
        }

       public boolean drag( int mx, int my ) { return hitTest(mx,my); }
       public boolean hitTest( int mx, int my )
        {
            if ( (hitted = (my>=y && my<y+h) ) )
            {
                double pos = ((double)(mx-x-h/2))/(double)(w-100-h);
                if (pos<0) pos=0; else if (pos>1) pos=1;
                val = (pos*(max-min))+min;
            }
            return hitted;
        };
        public void setVal( double o ) { val = o; }
        public double getVal() { return val; }
    }

    class Info extends GUIObject {
        int size;
        Info( String _text, String _name, int y0, int _size )
        {
            text=_text; h=20; y=y0; name = _name; size=_size;
            w=menuW; x=0;
        }
        public void paint( Graphics g, int sx, int sy, int sw ) {
            if (text!=null)
            {
                g.setColor( 0x7f7f7f );
                Font font = Font.getFont( Font.FACE_PROPORTIONAL, ((size&1)>0)?Font.STYLE_BOLD:Font.STYLE_PLAIN,
                        ((size/2)==0) ? Font.SIZE_SMALL : (((size/2)==2) ? Font.SIZE_LARGE : Font.SIZE_MEDIUM) );
                g.setFont(font);
                g.setColor( 0xffffff );
                g.drawString(text, x+sx+(w-font.stringWidth(text))/2, y+sy+border, 0);
            }
            updated=false;
        }
    }

    public MenuView(  ) {
        menuW = getWidth();
        menuH = getHeight();

        if (menuW>=480)
        {
            border = 5;
            try {
                nL = Image.createImage ( "/gui/normall.png" );
                nM = Image.createImage ( "/gui/normalm.png" );
                nR = Image.createImage ( "/gui/normalr.png" );
                pL = Image.createImage ( "/gui/pushedl.png" );
                pM = Image.createImage ( "/gui/pushedm.png" );
                pR = Image.createImage ( "/gui/pushedr.png" );

                nS = Image.createImage ( "/gui/normalslider.png" );
                pS = Image.createImage ( "/gui/pushedslider.png" );

                itemH = nM.getHeight()+border*2;
            } catch (Exception e) { nL = nM = nR = pL = pR = pM = null; }
        }
        else
        {
            border = 2;
            try {
                nL = Images.scaleDown(Image.createImage ( "/gui/normall.png" ));
                nM = Images.scaleDown(Image.createImage ( "/gui/normalm.png" ));
                nR = Images.scaleDown(Image.createImage ( "/gui/normalr.png" ));
                pL = Images.scaleDown(Image.createImage ( "/gui/pushedl.png" ));
                pM = Images.scaleDown(Image.createImage ( "/gui/pushedm.png" ));
                pR = Images.scaleDown(Image.createImage ( "/gui/pushedr.png" ));

                nS = Images.scaleDown(Image.createImage ( "/gui/normalslider.png" ));
                pS = Images.scaleDown(Image.createImage ( "/gui/pushedslider.png" ));

                itemH = nM.getHeight()+border*2;
            } catch (Exception e) { nL = nM = nR = pL = pR = pM = null; }
        }
        fontH = Font.getFont( Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_SMALL ).getHeight()+border*2;
        itemX=0;
        itemY=0;
    }
    
    public void addHeader( String t, String name )
    {
        if (itemX>0) itemY+=itemH; itemX=0;
        guiobjs.addElement( new Header(t,name,itemY) ); itemY+=fontH;
    }
    
    public void addInfo( String t, String name, int size )
    {
        if (itemX>0) itemY+=itemH; itemX=0;
        guiobjs.addElement( new Info(t,name,itemY, size) ); itemY+=fontH-(3-size/2);
    }

    public void addSlider( String t, String name, double min, double max, double val )
    {
        if (itemX>0) itemY+=itemH; itemX=0;
        guiobjs.addElement( new Slider(t,name,itemY,min,max,val) ); itemY+=itemH;
    }

    public void addButton( String t, String name, double w )
    {
        if (itemX>=1) { itemY+=itemH; itemX=0; }
        guiobjs.addElement( new Button(t,name,itemY,itemX,w) ); itemX+=w;
    }

    public void addLine( int y )
    {
        if (itemX>0) { itemY+=itemH; itemX=0; } itemY+=y;
    }
    
    public void setColor( String name, int col )
    {
        for ( int i = 0; i<guiobjs.size(); i++ ) 
        {
            GUIObject g = ((GUIObject)guiobjs.elementAt(i));
            if (g.equal(name)) g.setColor(col);
        }
    }

    public void setText( String name, String text )
    {
        for ( int i = 0; i<guiobjs.size(); i++ ) 
        {
            GUIObject g = ((GUIObject)guiobjs.elementAt(i));
            if (g.equal(name)) g.setText(text);
        }
    }

    public void setOn( String name, boolean on )
    {
        for ( int i = 0; i<guiobjs.size(); i++ )
        {
            GUIObject g = ((GUIObject)guiobjs.elementAt(i));
            if (g.equal(name)) g.setOn(on);
        }
    }

    public void setVal( String name, double v )
    {
        for ( int i = 0; i<guiobjs.size(); i++ )
        {
            GUIObject g = ((GUIObject)guiobjs.elementAt(i));
            if (g.equal(name)) g.setVal(v);
        }
    }
    
    public double getVal( String name )
    {
        double ret=0;
        for ( int i = 0; i<guiobjs.size(); i++ )
        {
            GUIObject g = ((GUIObject)guiobjs.elementAt(i));
            if (g.equal(name)) ret=g.getVal();
        }
        return ret;
    }
    
    public void paint( Graphics g )
    {
        int w = getWidth ();
        int h = getHeight ();
        if (bg!=null) {
            for ( int y=0; y<h; y+=bg.getHeight() )
            for ( int x=0; x<w; x+=bg.getWidth() )
            g.drawImage( bg, menuX+x, menuY+y, 0 );
        } else
        {
            g.setColor(0x000000);
            g.fillRect (0, 0, w, h);
        }
        for ( int i = 0; i<guiobjs.size(); i++ )
            ((GUIObject)guiobjs.elementAt(i)).paint(g, menuX, menuY, w);
    }

    public void pointerPressed( int x, int y )
    {
        for ( int i = 0; i<guiobjs.size(); i++ )
        {
            GUIObject obj = (GUIObject)guiobjs.elementAt(i);
            if ( obj.hitTest(x,y) ) onAction(obj.name);
        }
        repaint();
    }

    public void pointerDragged( int x, int y )
    {
        for ( int i = 0; i<guiobjs.size(); i++ )
        {
            GUIObject obj = (GUIObject)guiobjs.elementAt(i);
            if ( obj.drag(x,y) ) onAction(obj.name);
        }
        repaint();
    }

    public void pointerReleased( int x, int y ) {
        for ( int i = 0; i<guiobjs.size(); i++ )
            ((GUIObject)guiobjs.elementAt(i)).hitTest(-1000,-1000);
        repaint();

    }

    public void onAction( String s )
    {
    }

}
